package com.liit.util;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import com.liit.entity.UserFormMap;

public class PasswordHelper {
	private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
	private String algorithmName = "md5";
	private int hashIterations = 2;

	public void encryptPassword(UserFormMap userFormMap) {
		String salt = randomNumberGenerator.nextBytes().toHex();
		userFormMap.put("credentialsSalt", salt);
		String newPassword = new SimpleHash(algorithmName, userFormMap.get("password"), ByteSource.Util.bytes(userFormMap.get("accountName") + salt), hashIterations).toHex();
		userFormMap.put("password", newPassword);
	}

	public boolean validatePassword(UserFormMap userFormMap, String password) {
		String oldPassword = userFormMap.getStr("password");
		password = new SimpleHash(algorithmName, password, ByteSource.Util.bytes(userFormMap.get("accountName") + userFormMap.getStr("credentialsSalt")), hashIterations).toHex();
		if (oldPassword.equals(password)) {
			return true;
		} else {
			return false;
		}
	}

	public static void main(String[] args) {
		PasswordHelper passwordHelper = new PasswordHelper();
		UserFormMap userFormMap = new UserFormMap();
		userFormMap.put("password", "123456");
		userFormMap.put("accountName", "admin");
		passwordHelper.encryptPassword(userFormMap);
		System.out.println(userFormMap);
	}
}
